<?php
	/**
	 * @package AmpleHarvest
	 */
	use\ampleharvest\code;
	require_once dirname(dirname(dirname(__FILE__))) . '/config/cConfig.php';
	$codeConfig = new \ampleharvest\code\cConfig();
	
	require_once dirname(__DIR__).'/Tables/cTABLE_Drop_Down_Options.php';
	/**
	 * Includes the base class for Tables (uses base path from the cinfo file
	 */
	require_once $codeConfig->C_STR_DATABASE_LIB_PATH . "/Classes/cView.php";
	/**
	 * Includes the base class for columns(uses base path from the cinfo file
	 */
	require_once $codeConfig->C_STR_DATABASE_LIB_PATH . "/Classes/ColTypes.php";

	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 30 MAR 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cView_HarvestRequests extends cView
	{
		const C_INT_MAX_RESULTS_RETURNED = 500;
		const C_INT_DEFAULT_RESULTS_RETURNED = 100;
		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_RequestNum = null;
		public $m_int_COL_UserID = null;
		public $m_int_COL_URL = null;
		public $m_int_COL_KeyWords = NULL;
		public $m_int_COL_WholeSite = NULL;
		public $m_int_COL_DateRequested = null;
		public $m_int_COL_DateUpdated = null;
		public $m_int_COL_Status = null;
		public $m_int_COL_Description = null;
		public $m_int_COL_Frequency = null;
		public $m_int_COL_Sensitivity = null;
		public $m_int_COL_Risk = null;
		public $m_int_COL_Type = null;
		public $m_int_COL_Reason = null;
		public $m_int_COL_Comment = null;
		public $m_int_COL_Discussion = null;
		public $m_int_COL_Language = null;
		public $m_int_COL_Classification = null;
		public $m_int_COL_SCI = null;
		public $m_int_COL_Distro = null;
		public $m_int_COL_UserName = null;
		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "view_HarvestRequest";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of harvest requests.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_UserID = new cColVarChar("user_id");
			$obj_COL_UserID->set_Length(36);
			$obj_COL_UserID->set_Comment("This column holds the user GUID.");
			$obj_COL_UserID->set_Label("User GUID");

			$obj_COL_URL = new cColText("url");
			$obj_COL_URL->set_Comment("This column holds the Target URL.");
			$obj_COL_URL->set_Label("Source URL");

			$obj_COL_KeyWords = new cColText("key_words");
			$obj_COL_KeyWords->set_Comment("This column holds the key words for the request.");
			$obj_COL_KeyWords->set_Label("Keywords");

			$obj_COL_WholeSite = new cColTinyInt("whole_site");
			$obj_COL_WholeSite->set_Comment("This column determines if the user wants everything or not.");
			$obj_COL_WholeSite->set_Label("Whole Site");

			$obj_COL_DateRequested = new cColDateTime("date_requested");
			$obj_COL_DateRequested->set_Comment("This column holds the last run time.");
			$obj_COL_DateRequested->set_Label("Date Requested");

			$obj_COL_DateUpdated = new cColDateTime("date_updated");
			$obj_COL_DateUpdated->set_Comment("This column holds the next run time.");
			$obj_COL_DateUpdated->set_Label("Date Updated");

			$obj_COL_Status = new cColTinyInt("status");
			$obj_COL_Status->set_Comment("This column holds current request status.");
			$obj_COL_Status->set_Label("Status");

			$obj_COL_Description = new cColLongBlob("description");
			$obj_COL_Description->set_Comment("This column holds the request description.");
			$obj_COL_Description->set_Label("Description");

			$obj_COL_Frequency = new cColSmallInt("frequency");
			$obj_COL_Frequency->set_Comment("This column holds the harvest frequency.");
			$obj_COL_Frequency->set_Label("Frequency");

			$obj_COL_Sensitivity = new cColSmallInt("sensitivity");
			$obj_COL_Sensitivity->set_Comment("This column holds the sensitivity level.");
			$obj_COL_Sensitivity->set_Label("Sensitive");

			$obj_COL_Risk = new cColText("risk");
			$obj_COL_Risk->set_Comment("This column holds the risk level.");
			$obj_COL_Risk->set_Label("Risk");

			$obj_COL_Type = new cColSmallInt("type");
			$obj_COL_Type->set_Comment("This column holds the target type description.");
			$obj_COL_Type->set_Label("Source Type");

			$obj_COL_Reason = new cColLongBlob("reason");
			$obj_COL_Reason->set_Comment("This column holds the harvest request reason.");
			$obj_COL_Reason->set_Label("Justification");

			$obj_COL_Comment = new cColLongBlob("comment");
			$obj_COL_Comment->set_Comment("This column holds the harvest request comment section.");
			$obj_COL_Comment->set_Label("Comment");

			$obj_COL_Discussion = new cColLongBlob("discussion");
			$obj_COL_Discussion->set_Comment("This column holds the harvest request discussions.");
			$obj_COL_Discussion->set_Label("Discussion");

			$obj_COL_Language = new cColText("language");
			$obj_COL_Language->set_Comment("This column holds the URL target language.");
			$obj_COL_Language->set_Label("Language");

			$obj_COL_Classification = new cColVarChar("classification");
			$obj_COL_Classification->set_Length(36);
			$obj_COL_Classification->set_Comment("This column holds the classification of the Request.");
			$obj_COL_Classification->set_Label("Classification");

			$obj_COL_SCI = new cColVarChar("sci");
			$obj_COL_SCI->set_Length(100);
			$obj_COL_SCI->set_Comment("This column holds the SCI portion of the classification of the Request.");
			$obj_COL_SCI->set_Label("SCI");

			$obj_COL_Distro = new cColVarChar("distro");
			$obj_COL_Distro->set_Length(100);
			$obj_COL_Distro->set_Comment("This column holds the Distro portion of the classification of the Request.");
			$obj_COL_Distro->set_Label("Distro");

			$obj_COL_RequestNum = new cColInt("requestNum");
			$obj_COL_RequestNum->set_Comment("This column holds the Request Number.");
			$obj_COL_RequestNum->set_Label("Request #");

			$obj_COL_UserName = new cColVarChar("UserName");
			$obj_COL_UserName->set_Length(25);
			$obj_COL_UserName->set_Index(abs_Column::c_int_INDEX_UNIQUE);
			$obj_COL_UserName->set_Comment("This column holds the User Name of users.");
			$obj_COL_UserName->set_Label("User Name");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//			$this->m_obj_Connection->activate_Logging();
			//			$this->m_obj_Connection->activate_LogDebug();
			//			$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_UserID = $this->add_Column($obj_COL_UserID);
			$this->m_int_COL_URL = $this->add_Column($obj_COL_URL);
			$this->m_int_COL_KeyWords = $this->add_Column($obj_COL_KeyWords);
			$this->m_int_COL_WholeSite = $this->add_Column($obj_COL_WholeSite);
			$this->m_int_COL_DateRequested = $this->add_Column($obj_COL_DateRequested);
			$this->m_int_COL_DateUpdated = $this->add_Column($obj_COL_DateUpdated);
			$this->m_int_COL_Status = $this->add_Column($obj_COL_Status);
			$this->m_int_COL_Description = $this->add_Column($obj_COL_Description);
			$this->m_int_COL_Frequency = $this->add_Column($obj_COL_Frequency);
			$this->m_int_COL_Sensitivity = $this->add_Column($obj_COL_Sensitivity);
			$this->m_int_COL_Risk = $this->add_Column($obj_COL_Risk);
			$this->m_int_COL_Type = $this->add_Column($obj_COL_Type);
			$this->m_int_COL_Reason = $this->add_Column($obj_COL_Reason);
			$this->m_int_COL_Comment = $this->add_Column($obj_COL_Comment);
			$this->m_int_COL_Discussion = $this->add_Column($obj_COL_Discussion);
			$this->m_int_COL_Language = $this->add_Column($obj_COL_Language);
			$this->m_int_COL_Classification = $this->add_Column($obj_COL_Classification);
			$this->m_int_COL_SCI = $this->add_Column($obj_COL_SCI);
			$this->m_int_COL_Distro = $this->add_Column($obj_COL_Distro);
			$this->m_int_COL_RequestNum = $this->add_Column($obj_COL_RequestNum);
			$this->m_int_COL_UserName = $this->add_Column($obj_COL_UserName);
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Harvest Request to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the Query to check (GUID)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}

		/**
		 * Convert a Status Value into the Status Label
		 * @param string $str_Value Value of the status to convert
		 * @return string the Label Attatched to the Value if available
		 */
		public function convert_StatusValue2Label($str_Value)
		{
			$obj_Table_DropDownOptions = new cTABLE_Drop_Down_Options();
			return $obj_Table_DropDownOptions->convert_Value2Label(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS, $str_Value);
		}

		/**
		 * Gets the Status of the current Request in Text Form
		 * @return string Lable attatched to the current Status Value
		 */
		public function get_StatusTxt()
		{
			return $this->convert_StatusValue2Label($this->get_status());
		}

		public function get_CreateSQL()
		{
			return "CREATE VIEW ".$this->m_str_TableName." AS SELECT Harvest_Requests.*,`User_Manager`.users.`UserName` FROM `Harvest_Requests` LEFT JOIN  `User_Manager`.users ON `user_id` = `User_Manager`.users.id";
		}
	}
?>
